<?php

	class FTANews extends FTAObject {
		
		var $dateFormat;
		var $renderArticlesFunction;
		var $renderNewsTickerFunction;
		var $renderNewsListFunction;
		var $renderLatestNewsListFunction;
		var $news;
		
		function FTANews(){
			parent::__construct();
			
			global $db;	
			$this->cmsDocument = new CmsDocument($db);
			
			global $FTADateFormat;
			$this->dateFormat = (0 < strlen($FTADateFormat)) ? $FTADateFormat : "d F y";
						
		}
		
		function getNews($pageSize = null, $pageNumber = null, $category = null, $sort = null, $direction = null){
			
			$this->cmsDocument->flush();
			$this->cmsDocument->setActiveDocuments();
			$this->cmsDocument->addDocumentType('docNews');
			if(!is_null($category)){
				$this->cmsDocument->addConstraint("categorycode", $category);
			}
			if(!is_null($sort)){
				if(!is_null($direction)){
					$this->cmsDocument->addSort($sort, $direction);
				} else {
					$this->cmsDocument->addSort($sort);
				}
			} else {
				$this->cmsDocument->addSort("documentyear","DESC");
			}
			
			$this->cmsDocument->setPageSize($pageSize);
			$this->cmsDocument->setPageNumber($pageNumber);
			
			$this->cmsDocument->resetIterator();
			
			$allDocuments = $this->cmsDocument->getAllDocuments();
			
			if (!$allDocuments)
				$allDocuments = array();
				
			return $allDocuments;			
		}
				
		function openNews($newsIdentifier){

			$this->cmsDocument->flush();
			$this->cmsDocument->setActiveDocuments();
			$this->cmsDocument->addDocumentType('docNews');
			
			if (is_numeric( $newsIdentifier ) &&  0 < ($newsIdentifier * 1)){
				$this->cmsDocument->addConstraint('documentcode',$newsIdentifier);	
			} else {
				$this->cmsDocument->addConstraint('<<md.heading>>',$newsIdentifier);
			}
			
			$this->cmsDocument->setPageSize(1);
			
			$this->cmsDocument->resetIterator();
			
			$allDocuments = $this->cmsDocument->getAllDocuments();

			//echo $this->cmsDocument->documentIteratorQuery();
			
			if($this->cmsDocument->totalDocuments() == 1) {
				//$this->news = $this->cmsDocument->nextDocument();
				$this->news = $allDocuments[0];
				//print_r($this->news);
				return $allDocuments[0];
			} else {
				$this->news = array();		
				return false;
			}
						
		}
		
		
/////		

		
		function renderNewsList($pageSize = null, $pageNumber = null, $category = null, $sort = null, $direction = null){ 
		
			$news = $this->getNews($pageSize, $pageNumber, $category, $sort, $direction);
		
			if (0 == count($news))	return;
			
			if(is_null($this->renderNewsListFunction)) {
				
				global $FTANewsListItemRenderFunction;
				if(!isset($FTANewsListItemRenderFunction)){
					$FTANewsListItemRenderFunction = '$o=fta_getSharedNewsObject(); echo $o->renderNewsListItemFunction($item);';
				}
	
				$this->renderNewsListFunction  = create_function('$item', $FTANewsListItemRenderFunction );
			}
				
			//we need to map the links and do some funky scope wrandling..
			fta_widgetList($news,"newsList","altItem","firstItem", "lastItem",$this->renderNewsListFunction);
		}
		
		function renderNewsSubNavigation(){
			$articles = $this->getNews();

			if(0 == count($articles))
				return;				

			$articles = array_map(array($this,"map_newsCmsDocumentRecordToLinkList"),$articles);
			echo fta_widgetNavigationList($articles);		
						
		}
		
		function map_newsCmsDocumentRecordToLinkList($o){

			global $fta_;
						
			$oo = new stdClass();
			$oo->destination = $fta_->getPageScope() . date("Y/m/d/",@strtotime($o->documentyear))  . urlFragmentClean($o->heading) . "/";
			$oo->text = $o->heading;
			return $oo;			
		}
				
		
////
		function renderNewsTicker() {
			// get a set amount of news items and stick em in a ticker.	
			
			$newsLimit = (isset($FTANewsArticleLimit)) ? $FTANewsArticleLimit : 10 ;
			$newsArticles = $this->getNews();
			$newsArticles = array_slice($newsArticles,0,$newsLimit);

			if(is_null($this->renderNewsTickerFunction)) {	
				$this->renderNewsTickerFunction  = create_function('$item', '$o=fta_getSharedNewsObject(); echo $o->renderNewsTickerItemsFunction($item);' );
			}

			
?>
<style>
#newsItemsScrolleyList .scrolleyList {
	width: <?= count($newsArticles) * 288 ?>px;
}
#newsItemsScrolleyList ul.scrolleyList {
	width: <?= count($newsArticles) * 288 ?>px;
}
</style>
<div id="newsItemsScrolleyList">
<?			
			fta_widgetList(
				$newsArticles,'scrolleyList',null,null,null, $this->renderNewsTickerFunction
			);
?>
</div>
<script>
Event.observe(window,'load',function(){
	var fa = $('newsItemsScrolleyList');
	var gs = new GRVScroller(fa,$$("#newsItemsScrolleyList ul.scrolleyList li"));
	fa.scrolley = gs;
});
</script>
<?			
		}

		
		
		function renderLatestNewsList(){ 
			global $FTANewsLatestNewsListLimit;
			$newsLimit = (isset($FTANewsLatestNewsListLimit)) ? $FTANewsLatestNewsListLimit : 10 ;
			$news = $this->getNews();
			$news = array_slice($news,0,$newsLimit);
			
			if (0 == count($news))	return;
			
			if(is_null($this->renderLatestNewsListFunction)) {
				
				global $FTANewsLatestNewsListItemRenderFunction;
				if(!isset($FTANewsLatestNewsListItemRenderFunction)){
					$FTANewsLatestNewsListItemRenderFunction = '$o=fta_getSharedNewsObject(); echo $o->renderLatestNewsListItemFunction($item);';
				}
	
				$this->renderLatestNewsListFunction  = create_function('$item', $FTANewsLatestNewsListItemRenderFunction );
			}
				
			//we need to map the links and do some funky scope wrandling..
			fta_widgetList($news,"linksList","altItem","firstItem", "lastItem",$this->renderLatestNewsListFunction);
		}
		
		
		function renderNewsTickerItemsFunction($item) {
?>
<div class="newsDate"> 
	<?= date("m",strtotime($item->documentyear)) ?> 
	<div class="newsMonth"> <?= date("y",strtotime($item->documentyear)) ?></div>
</div>
<div class="newsBlurb"><?= $item->shortdescription ?> &hellip;</div>
<?
		}
		
		
		function renderNewsHeading(){
			if(is_null($this->news))
				return;				
			
			echo fta_widgetH1Tag($this->news->heading, "");				
		}
		
		function renderNewsDate(){
			if(is_null($this->news))
				return;				
			
			echo fta_widgetH5Tag(
				date($this->dateFormat,strtotime($this->news->documentyear)), ""
			);				
		}		
        // JRM 17-06-2010
        // Added for LCT 
        // Returning Date wrapped in H2
		function renderNewsDateH2(){
			if(is_null($this->news))
				return;				
			
			echo fta_widgetH2Tag(
				date($this->dateFormat,strtotime($this->news->documentyear)), ""
			);				
		}
		
		function renderNewsContent(){
			if(is_null($this->news))
				return;		
			echo $this->news->content;			
		}
		
		function renderNewsImage($elementId = 11742, $filetypecode = 2, $class = 'imgL' ){
			if(is_null($this->news))
				return;	
			if($this->news->phone == '') // phone used for main image on News record
				return;	
			echo "<img class=\"" . $class . "\" src=\"/html/blob.php?documentCode=" . $this->news->documentcode . "&filetypecode=" . $filetypecode . "&elementId=" . $elementId . "\" alt=\"" . $this->news->heading . "\" />";			
		}

        // JRM 17-06-2010
        // PDF Download Link
        function renderNewsPdfLink($elementId = 20084, $fileTypeCode = 1){
			if(is_null($this->news))
				return;			
			if($this->news->email == '') // email used for pdf attachment on News record
				return;	
            global $db;
            $fileRecord = getBinaryRecordForDocumentCodeAndElementId($this->news->documentcode, $elementId ,$fileTypeCode, $db);
            echo "/html/blob.php/" . $fileRecord['filename'] . "?attach=0&documentCode=" . $this->news->documentcode . "&elementId=" . $elementId;
        }
		
		
/////		
		function renderNewsTitle(){
			if(is_null($this->news))
				return;			
?>
<title><?= $this->news->pagetitle ?></title>
<?			
		}
		
		function renderNewsKeywords(){
			if(is_null($this->news))
				return;			
?>
<meta name="Keywords" content="<?= $this->news->keywords ?>" />
<?				
		}
		
		function renderNewsDescription(){
			if(is_null($this->news))
				return;
?>
<meta name="Description" content="<?= $this->news->description ?>" />
<?			

		}
				
		
		
	}
	
	
//

	// news content
	// news list/filter
	
	function fta_getNewsSubNavigation(){		
		ob_start();
		$newsObject = fta_getSharedNewsObject();
		$newsObject->renderNewsSubNavigation();
		return ob_get_clean();				
	}
	
	
	function fta_getNewsList($pageSize = null, $pageNumber = null, $category = null, $sort = null, $direction = null){		
		ob_start();
		$newsObject = fta_getSharedNewsObject();
		$newsObject->renderNewsList($pageSize, $pageNumber, $category, $sort, $direction);
		return ob_get_clean();				
	}
	
	
	function fta_getLatestNewsList(){		
		ob_start();
		$newsObject = fta_getSharedNewsObject();
		$newsObject->renderLatestNewsList();
		return ob_get_clean();				
	}
	
	
	function fta_getNewsDate($newsIdentifier){
		ob_start();
		fta_newsOpenNewsAndInvoke($newsIdentifier, 'renderNewsDate');		
		return ob_get_clean();			
	}
	
	function fta_getNewsHeading($newsIdentifier){
		ob_start();
		fta_newsOpenNewsAndInvoke($newsIdentifier, 'renderNewsHeading');		
		return ob_get_clean();				
	}
	
///

	function fta_getNewsTitle($newsIdentifier){
		ob_start();
		fta_newsOpenNewsAndInvoke($newsIdentifier, 'renderNewsTitle');		
		return ob_get_clean();			
	}
	
	function fta_getNewsKeywords($newsIdentifier){
		ob_start();
		fta_newsOpenNewsAndInvoke($newsIdentifier, 'renderNewsKeywords');		
		return ob_get_clean();			
	}
	
	function fta_getNewsDescription($newsIdentifier){
		ob_start();
		fta_newsOpenNewsAndInvoke($newsIdentifier, 'renderNewsDescription');		
		return ob_get_clean();			
	}
	
	function fta_getNewsMetaData($newsIdentifier){
		$metaData="";
		$metaData .= fta_getNewsTitle($newsIdentifier);
		$metaData .= fta_getNewsKeywords($newsIdentifier);
		$metaData .= fta_getNewsDescription($newsIdentifier);
		return $metaData;	
	}
	
	
////

	function fta_getNewsContent($newsIdentifier){
		ob_start();
		fta_newsOpenNewsAndInvoke($newsIdentifier, 'renderNewsContent');		
		return ob_get_clean();			
	}
	
	
	function fta_getNewsImage($newsIdentifier, $elementId = 11742, $filetypecode = 2){
		ob_start();
		$newsObject = fta_getSharedNewsObject();
		$newsObject->openNews( $newsIdentifier );
		$newsObject->renderNewsImage($elementId, $filetypecode);
		return ob_get_clean();
	}
	
	
	function fta_getNews($newsIdentifier){
		$news = "";
		$news .= fta_getNewsHeading($newsIdentifier);
		$news .= fta_getNewsDate($newsIdentifier);
		$news .= fta_getNewsContent($newsIdentifier);
		return $news;
	}
	
	
	function fta_getNewsTicker(){
		ob_start();
		$newsObject = fta_getSharedNewsObject();
		$newsObject->renderNewsTicker();
		return ob_get_clean();		
	}
	
	//this will rely on voodoo..
	function fta_getNewsRSSLink(){}
	
//////	
	
	function fta_newsOpenNewsAndInvoke($newsIdentifier,$method){
		$newsObject = fta_getSharedNewsObject();
		if ( $newsObject->openNews( $newsIdentifier ) ) {
			$newsObject->$method();
		}
	}
		
	function fta_getNewsIdentifierFromRequestOrDefault($defaultNewsIdentifier=null){
		$newsIdentifier = extractStringWithKeyFromArray('REQUEST_URI',$_SERVER,null);
		
		// we need to subtract the page scope from the passed in url
		global $fta_;
		$pageScope = $fta_->getPageScope();

		if( preg_match("#^".$pageScope."(.*)/$#",$newsIdentifier,$matches) ){
			$newsIdentifier = urlFragmentCleanRestore($matches[1]);
			
			$nis = split("/",$newsIdentifier);
			if (4 == count($nis)){
				$newsIdentifier = $nis[3];
			} else {
				$newsIdentifier = $defaultNewsIdentifier;
			}

		} else {
			$newsIdentifier = $defaultNewsIdentifier;
		}

		$newsObject = fta_getSharedNewsObject();
				
		if ($newsIdentifier == $defaultNewsIdentifier && is_null($defaultNewsIdentifier)){
			$articles = $newsObject->getNews();
			if(0 <count($articles)){
				$newsIdentifier = $articles[0]->documentcode;
			}
		}
		

		if (!is_null($newsIdentifier) && $newsObject->openNews( $newsIdentifier )){
			return $newsIdentifier;
		}
		
	}
	
	/**
	 * Gets the fta obejct.. these are singletons really..
	 *
	 * @return FTAWebPage
	 */
	function fta_getSharedNewsObject(){

		global $fta_;

		global $FTANewsClass;
		
		if(!is_null($FTANewsClass) && "FTANews" != $FTANewsClass){
		} else {
			$FTANewsClass = "FTANews";
		}
			
		if (!array_key_exists('ftaNews',$fta_)){
			$fta_->ftaNews = new  $FTANewsClass();
		}
		return $fta_->ftaNews;
	}
